<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Invite extends Admin_Controller {
		private $dbconn;
		private $admim_db;
		private $Referrals;

		public function __construct(){  
			parent::__construct();
			$this->dbconn = $this->load->database('default',true);
			$this->admim_db = $this->load->database('admin',true);
			$this->load->library('Referrals');
			$this->Referrals = $this->referrals;
		}

		public function index(){
			$get		=	$this->input->get();
			$field		=	"create_time";//默认排序
			$orderin	=	'desc';
			//指定排序
			if(isset($get['field'])){
				$field = $get['field'];
				if(isset($get['order']) && !empty($get['order'])){
					$orderin = $get['order'];
				}
			}
			$order	=	$field.' '.$orderin;
			$url_	=	"&field=".$field.'&order='.$orderin;
	
			$master	=	isset($get['master'])?$get['master']:"";
			$slave	=	isset($get['slave'])?$get['slave']:"";
			
			$where	=	" 1=1";
			$where	.=	!empty($master)?" and master ='$master'":"";
			$where	.=	!empty($slave)?" and slave ='$slave'":"";
			$nowpage=	1;

			$this->load->library('pagination');//加载分页标签
			if(isset($get[$this->pagination->query_string_segment])){
				if(!empty($get[$this->pagination->query_string_segment])){
					$nowpage	=	$get[$this->pagination->query_string_segment];
				}
			}

			$vip_arr	=	$this->Referrals->getInviteAllVip();
			$vip_list	=	array();
			if(!empty($vip_arr))foreach($vip_arr as $arr){
				$vip_list[$arr['uid']]	=	$arr['uid'];
			}
	
			//加载分页数据model
			$this->load->model('Pagequery_model');
			$table		=	"cfd_invite";
			$tmp_log	= $this->Pagequery_model->page_query($this->dbconn,$table,$order,$this->pagesize,$where,$nowpage);

			//加载文字处理类
			$this->load->library('ipoperation');
			if(!empty($tmp_log))foreach($tmp_log as &$items){
				$items['vip']	=	!empty($vip_list[$items['master']])?"<font color=red>vip经纪人</font>":"普通经纪人";	
				$this->ipoperation->getIPArea($items['ip']);
				$country	=	$this->ipoperation->country;
				$province	=	$this->ipoperation->province;
				$city		=	$this->ipoperation->city;
				if(empty($country)&&empty($province)&&empty($city))$items['area']="-------";
				else $items['area']	=	$country." ".$province." ".$city;
			}

			$config['base_url']	  =	$this->get_url(array($this->pagination->query_string_segment));
			$config['total_rows'] = !empty($tmp_log)?$tmp_log[0]['Total']:0;
			$config['per_page']   = $this->pagesize;
			$config['do_jump']    = true;
			$config['use_page_numbers']=true;
			$this->pagination->initialize($config); 
			$this->data['page_html']	=	$this->pagination->create_links();
			$this->data['page']			=	'invite/index';
			$this->data['page_data']	=	$tmp_log;
			$this->data['url']			=	$this->get_url(array('order','field',$this->pagination->query_string_segment));
			$this->data['get']			=	$get;
			$this->data['field']		=	$field;
			$this->data['order']		=	$orderin;
			$this->load->view('layout',$this->data);
		}

		public function vip(){
			$get		=	$this->input->get();
			$field		=	"create_time";//默认排序
			$orderin	=	'desc';
			//指定排序
			if(isset($get['field'])){
				$field = $get['field'];
				if(isset($get['order']) && !empty($get['order'])){
					$orderin = $get['order'];
				}
			}
			$order	=	$field.' '.$orderin;
			$url_	=	"&field=".$field.'&order='.$orderin;
	
			$uid	=	isset($get['uid'])?$get['uid']:"";
			$phone	=	isset($get['phone'])?$get['phone']:"";

			
			$where	=	" 1=1";
			$where	.=	!empty($uid)?" and uid ='$uid'":"";
			$where	.=	!empty($phone)?" and phone ='$phone'":"";
			$nowpage=	1;

			$this->load->library('pagination');//加载分页标签
			if(isset($get[$this->pagination->query_string_segment])){
				if(!empty($get[$this->pagination->query_string_segment])){
					$nowpage	=	$get[$this->pagination->query_string_segment];
				}
			}

	
			//加载分页数据model
			$this->load->model('Pagequery_model');
			$table		=	"cfd_invite_vip";
			$tmp_log	= $this->Pagequery_model->page_query($this->dbconn,$table,$order,$this->pagesize,$where,$nowpage);

			$config['base_url']	  =	$this->get_url(array($this->pagination->query_string_segment));
			$config['total_rows'] = !empty($tmp_log)?$tmp_log[0]['Total']:0;
			$config['per_page']   = $this->pagesize;
			$config['do_jump']    = true;
			$config['use_page_numbers']=true;
			$this->pagination->initialize($config); 
			$this->data['page_html']	=	$this->pagination->create_links();
			$this->data['page']			=	'invite/vip';
			$this->data['page_data']	=	$tmp_log;
			$this->data['url']			=	$this->get_url(array('order','field',$this->pagination->query_string_segment));
			$this->data['get']			=	$get;
			$this->data['field']		=	$field;
			$this->data['order']		=	$orderin;
			$this->load->view('layout',$this->data);
		}

		/**
		* 详细信息
		*/
		public function detail(){
			$get	=	$this->input->get();
			$field	=	"cny";//默认排序
			$orderin=	'desc';
			//指定排序
			if(isset($get['field'])){
				$field = $get['field'];
				if(isset($get['order']) && !empty($get['order'])){
					$orderin = $get['order'];
				}
			}
			$order	=	$field.' '.$orderin;
			$url_	=	"&field=".$field.'&order='.$orderin;
			$master	=	isset($get['master'])?$get['master']:"";
			$slave	=	isset($get['slave'])?$get['slave']:"";
			
			$where	=	" 1=1";
			$where	.=	!empty($master)?" and master ='$master'":"";
			$where	.=	!empty($slave)?" and slave ='$slave'":"";
			$nowpage=	1;

			$this->load->library('pagination');//加载分页标签
			if(isset($get[$this->pagination->query_string_segment])){
				if(!empty($get[$this->pagination->query_string_segment])){
					$nowpage	=	$get[$this->pagination->query_string_segment];
				}
			}
			
			$total	=	array('cny'=>0,'trade_cny'=>0);
			if(!empty($master)){
				$total	=	$this->Referrals->getInviteCnyByMaster($master);
			}
	
			//加载分页数据model
			$this->load->model('Pagequery_model');
			$table		=	"cfd_invite_details";
			$tmp_log	= $this->Pagequery_model->page_query($this->dbconn,$table,$order,$this->pagesize,$where,$nowpage);

			$config['base_url']	  =	$this->get_url(array($this->pagination->query_string_segment));
			$config['total_rows'] = !empty($tmp_log)?$tmp_log[0]['Total']:0;
			$config['per_page']   = $this->pagesize;
			$config['do_jump']    = true;
			$config['use_page_numbers']=true;
			$this->pagination->initialize($config); 
			$this->data['page_html']	=	$this->pagination->create_links();
			$this->data['page']			=	'invite/details';
			$this->data['page_data']	=	$tmp_log;
			$this->data['url']			=	$this->get_url(array('order','field',$this->pagination->query_string_segment));
			$this->data['get']			=	$get;
			$this->data['total']		=	$total;
			$this->data['master']		=	$master;
			$this->load->view('layout',$this->data);
		}

		/**
		* VIP经纪人盈利数据
		*/
		public function vipDetail(){
			$get	=	$this->input->get();
			$master	=	isset($get['master'])?$get['master']:"";
			$sdate	=	isset($get['sdate'])?$get['sdate']:"";
			$edate	=	isset($get['edate'])?$get['edate']:"";
			if(empty($master)){
				$this->alertMsg("VIP经纪人不存在");
			}

			$vip	=	$this->Referrals->getInviteVip($master);
			if(empty($vip)){
				$this->alertMsg("VIP经纪人不存在");
			}
			
			$where	=	" master ='$master'";
			$nowpage=	1;
			$where	.=	!empty($sdate)?" and date >='$sdate'":"";
			$where	.=	!empty($edate)?" and date <='$edate'":"";

			$this->load->library('pagination');//加载分页标签
			if(isset($get[$this->pagination->query_string_segment])){
				if(!empty($get[$this->pagination->query_string_segment])){
					$nowpage	=	$get[$this->pagination->query_string_segment];
				}
			}
			
			$total	=	array('cny'=>0,'trade_cny'=>0);
	
			//加载分页数据model
			$this->load->model('Pagequery_model');
			$table		=	" cfd_report_user_trade";
			$order		=	"date desc";
			$tmp_log	= $this->Pagequery_model->page_query($this->admim_db,$table,$order,$this->pagesize,$where,$nowpage);
			
			$this->load->library('Statistics');
			$cny	=	0;
			if(!empty($tmp_log))foreach($tmp_log as &$items){
				$items['cny']	=	bcadd($items['yk'],$items['ticket_yk'],2);
				$where_data		=	array('slave'=>$items['uid'],'date'=>$items['date']);
				$invite			=	$this->referrals->getInviteDetails($where_data);
				$items['cny']	=	!empty($invite)?bcsub($items['cny'],$invite['cny'],2):$items['cny'];
				$cny			=	bcadd($items['cny'],$cny,2);
			}

			$config['base_url']	  =	$this->get_url(array($this->pagination->query_string_segment));
			$config['total_rows'] = !empty($tmp_log)?$tmp_log[0]['Total']:0;
			$config['per_page']   = $this->pagesize;
			$config['do_jump']    = true;
			$config['use_page_numbers']=true;
			$this->pagination->initialize($config); 
			$this->data['page_html']	=	$this->pagination->create_links();
			$this->data['page']			=	'invite/vip_details';
			$this->data['page_data']	=	$tmp_log;
			$this->data['url']			=	$this->get_url(array('order','field',$this->pagination->query_string_segment));
			$this->data['get']			=	$get;
			$this->data['cny']			=	$cny;

			$this->load->view('layout',$this->data);
		}

		/**
		* VIP经纪人提成
		*/
		public function vipCommission(){
			$get	=	$this->input->get();
			$master	=	isset($get['master'])?$get['master']:"";
			//$sdate	=	isset($get['sdate'])?$get['sdate']:"";
			//$edate	=	isset($get['edate'])?$get['edate']:"";
			
			$where	=	"1=1";
			$where	.=	!empty($master)?" and master ='$master'":"";
			$nowpage=	1;
			//$where	.=	!empty($sdate)?" and date >='$sdate'":"";
			//$where	.=	!empty($edate)?" and date <='$edate'":"";

			$this->load->library('pagination');//加载分页标签
			if(isset($get[$this->pagination->query_string_segment])){
				if(!empty($get[$this->pagination->query_string_segment])){
					$nowpage	=	$get[$this->pagination->query_string_segment];
				}
			}
				
			//加载分页数据model
			$this->load->model('Pagequery_model');
			$table		=	" cfd_invite_vip_details";
			$order		=	"create_time desc";
			$tmp_log	= $this->Pagequery_model->page_query($this->dbconn,$table,$order,$this->pagesize,$where,$nowpage);
			

			$config['base_url']	  =	$this->get_url(array($this->pagination->query_string_segment));
			$config['total_rows'] = !empty($tmp_log)?$tmp_log[0]['Total']:0;
			$config['per_page']   = $this->pagesize;
			$config['do_jump']    = true;
			$config['use_page_numbers']=true;
			$this->pagination->initialize($config); 
			$this->data['page_html']	=	$this->pagination->create_links();
			$this->data['page']			=	'invite/vip_commission';
			$this->data['page_data']	=	$tmp_log;
			$this->data['url']			=	$this->get_url(array('order','field',$this->pagination->query_string_segment));
			$this->data['get']			=	$get;

			$this->load->view('layout',$this->data);
		}
}